package com.marketingexpert.controller.streammgnt;

import java.io.PrintWriter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONArray;

import org.apache.log4j.Logger;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.Controller;

import com.marketingexpert.service.snsmgnt.SnsService;
import com.marketingexpert.service.snsmgnt.SnsServiceFactory;

public class LoadHotThemeController implements Controller {

	private Logger logger = Logger.getLogger(LoadHotThemeController.class);
	
	private SnsServiceFactory snsServiceFactory;
	
	public ModelAndView handleRequest(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		
		
		response.setHeader("Pragma", "No-cache"); 
		response.setHeader("Cache-Control", "no-cache"); 
		response.setHeader("Cache-Control", "no-store"); 
		response.setDateHeader("Expires", 0);  
		PrintWriter out = response.getWriter(); 	
		
		String type = request.getParameter("type");
		
		try {
		SnsService snsService = getSnsServiceFactory().getService(SnsServiceFactory.SINA);
	

		JSONArray timeline = null;
		
		timeline = snsService.getHotTrendsTimeline(type,0);
		response.setContentType("application/x-json;charset=GBK"); 
		out.print(timeline);
		return null;
		} catch(Exception e) {
			logger.error(e.getMessage(), e);
			response.setContentType("text/html;charset=GBK"); 
			out.print("error");
			return null;
		}
		
	}

	public SnsServiceFactory getSnsServiceFactory() {
		return snsServiceFactory;
	}

	public void setSnsServiceFactory(SnsServiceFactory snsServiceFactory) {
		this.snsServiceFactory = snsServiceFactory;
	}
	
	

}
